﻿--[[
    @brief    二维码登录逻辑处理
    @Author    jameszhai
    @date    2017-05-23
--]]

EQRView = 
{
    sQRPanel = "pnlQRCode";
    sQRTips  = "rtbTips";
    sQRTipsBack = "rtbTipsBack";
    sQRTimeOutTips = "rtbTimeOutTips";
    sQRImageBack = "btnQRPict";
    sQRImage = "btnQRPict\\pnlQRPict";
    sQRMask  = "btnQRPict\\pnlQRMask";
    sBtnBack = "btnBack";
    sTxtBMTips = "txtBlackMachineTips";
    sQRIcon  = "btnQRPict\\pnlLogo";

    sDlgTips = "login\\QRTipsWnd.xml";

    sQRLoadingBackground = "ONEIMAGE:client_login\\loading\\loading_mid.tfc;Center;0";
    sQRLoadFailedBackground = "ONEIMAGE:client_login\\qrcode_fail.png;Stretch;0;";

    sQRTipsFormat = "<f c=\"0xffffff\" al=\"center\">%s</f><br>"..
                    "<f c=\"0x29b9f6\" al=\"center\"><a>%s</a></f>";
    sQRImageBackgroundColor = "COLOR:0xFFFFFF";
};

EQRTipText = 
{
    sScanTips = "<f c=\"0xFFFFFF\" al=\"center\">"..LoadStr("WE_GAME_LOGIN_TIPS_TEXT_A").."<f c=\"0x35a3d2\">"..LoadStr("WE_GAME_LOGIN_TIPS_TEXT_B").."</f>"..LoadStr("WE_GAME_LOGIN_TIPS_TEXT_C").."</f>";
    sTimeout  = "<f c=\"0xFFFFFF\" al=\"center\">"..LoadStr("WE_GAME_LOGIN_TIME_OUT_TEXT_A").."<f c=\"0x35a3d2\"><a>"..LoadStr("WE_GAME_LOGIN_TIME_OUT_TEXT_B").."</a></f></f>";
    sGetQRFail  = "<f c=\"0xFFFFFF\" al=\"center\">"..LoadStr("WECHAT_QRCODE_QRCODE_GET_FAILED").."<f c=\"0x35a3d2\"><a>"..LoadStr("WECHAT_QRCODE_REFRESH").."</a></f></f>";
    sWaitingConfirm = "<f c=\"0xFFFFFF\" al=\"center\">"..LoadStr("WECHAT_QRCODE_USE_MBOILEPHONE").."</f>";
};

TQRLogin =
{
    InitView = function(nRootID)
        TQRLogin.nRootID = nRootID;
    end;

    UnInitView = function()
        TQRLogin.nRootID  = 0;

        TQRLogin.pQRView  = nil;
        TQRLogin.pQRImage = nil;
        TQRLogin.pQRTips  = nil;
        TQRLogin.pQRBMTips = nil;
        TQRLogin.pQRImageBack = nil;
        TQRLogin.pQRMask = nil;
        TQRLogin.pQRTimeOutTips = nil;
        TQRLogin.pQRTipsBack = nil;
        TQRLogin.nQRIcon = Tenio.INVALID_CONTROLID;

        if nil ~= TQRLogin.pTipsDlg then
            layout_mgr.remove_ctrl(TQRLogin.pTipsDlg:GetControlID());
            TQRLogin.pTipsDlg = nil;
        end;
    end;

    CheckUI = function()
        if nil ~= TQRLogin.pQRView then
            return true;
        end;

        TQRLogin.pQRView = ui_utility.get_ctrl_ptr_by_path(
                                TQRLogin.nRootID, EQRView.sQRPanel);

        if nil == TQRLogin.pQRView then
            TGPLog.LogE("QRView", "Get qr panel fail.");
            return false;
        end;

        local     nTempID = TQRLogin.pQRView:GetControlID();

        TQRLogin.pQRTimeOutTips = ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sQRTimeOutTips);
        if nil == TQRLogin.pQRTimeOutTips then
            TGPLog.LogE("QRView", "Get qr timeout tip fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(TQRLogin.pQRTimeOutTips:GetControlID(),
                                      "OnRTBClickHyperLink", "TQRLogin.OnClkTimeOutLink");

        TQRLogin.pQRTips = ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sQRTips);
        if nil == TQRLogin.pQRTips then
            TGPLog.LogE("QRView", "Get qr panel fail.");
            return false;
        end;

        TQRLogin.pQRTipsBack = ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sQRTipsBack);
        if nil == TQRLogin.pQRTipsBack then
            TGPLog.LogE("QRView", "Get qr back fail.");
            return false;
        end;

        ui_utility.setproperty_by_id(TQRLogin.pQRTips:GetControlID(),
                                      "OnRTBClickHyperLink", "TQRLogin.OnClkLink");

        TQRLogin.pQRMask  = ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sQRMask);
        if nil == TQRLogin.pQRMask then
            TGPLog.LogE("QRView", "Get qr mask fail.");
            return false;
        end;

        TQRLogin.pQRImageBack = ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sQRImageBack);
        if nil == TQRLogin.pQRImageBack then
            TGPLog.LogE("QRView", "Get qr image bk fail.");
            return false;
        end;

        TQRLogin.pQRImage = ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sQRImage);
                if nil == TQRLogin.pQRImage then
            TGPLog.LogE("QRView", "Get qr image fail.");
            return false;
        end;

        ui_utility.setproperty_by_id(TQRLogin.pQRImage:GetControlID(), "OnClick",
                                     "TQRLogin.OnClkQRImage");

        local     nBtnBack = ui_utility.get_ctrl_id_by_path(nTempID, EQRView.sBtnBack);

        if nil == nBtnBack or 0 == nBtnBack then
            TGPLog.LogE("QRView", "Get back control fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(nBtnBack, "OnClick", "TQRLogin.OnClkBack");

        local device_xone = comm_center.call_service("Svr_LoginMgr_IsDeviceXOne");
        if device_xone ~= nil and device_xone.is_xone ~= nil and device_xone.is_xone~= 0 then
            ui_utility.setproperty_by_id(nBtnBack, "Visible", "false");
        end;

        local     nTipsDlg = layout_mgr.restore_layout(EQRView.sDlgTips);

        if nil == nTipsDlg or 0 == nTipsDlg then
            TGPLog.LogE("QRView", "Create qr dialog fail.");
            return false;
        end;

        TQRLogin.pTipsDlg = ui_utility.get_ctrl_ptr_by_id(nTipsDlg);
        if nil == TQRLogin.pTipsDlg then
            TGPLog.LogE("QRView", "Get qr dialog fail.");
            return false;
        end;

        TQRLogin.nQRIcon = ui_utility.get_ctrl_id_by_path(nTempID, EQRView.sQRIcon);
        if 0 ~= TQRLogin.nQRIcon then
            TQRLogin.SetQRIcon(login_qrcode_config.qrcode_config_table.qrcode_icon);
        end

        TQRLogin.pQRBMTips  =    ui_utility.get_ctrl_ptr_by_path(nTempID, EQRView.sTxtBMTips);
        if TQRLogin.pQRBMTips ~= nil then
            TQRLogin.pQRBMTips:Show(FALSE);
        end;
        
        TQRLogin.pQRMask:Show(FALSE);
        return true;
    end;

    ShowView = function()
        if TLoginView ~= nil then
            TLoginView.ShowCapsTips(false);
        end;
        if TQRLogin.CheckUI() then
            TQRLogin.pQRView:Show(TRUE);
        else
            TGPLog.LogE("QRLogin", "Check UI fail.");
        end;

        TQRLogin.ShowQRBMTips(TLoginModule.GetBlackMachineState(), TLoginModule.GetBlackMachineAlertText());

        if nil ~= TQRLogin.pTipsDlg then
            local     zParam = comm_center.call_service("Svr_GetLoginWnd", {});

            if nil == zParam["window"] then
                TGPLog.LogE("QRLogin", "Get login window fail.");
                return;
            end;

            local     pMain = ui_utility.get_ctrl_ptr_by_id(zParam["window"]);

            if nil ~= pMain then
                local     rtMain  = pMain:GetRectEx(TRUE,TRUE);
                local     rtTips  = TQRLogin.pTipsDlg:GetRectEx(FALSE,TRUE);
                local     nWidth  = rtTips.right  - rtTips.left;
                local     nHeight = rtTips.bottom - rtTips.top;

                rtTips.top       = rtMain.top + (rtMain.bottom - rtMain.top - nHeight) / 2;
                rtTips.bottom = rtTips.top + nHeight;
                rtTips.left   = rtMain.right + 3;
                rtTips.right  = rtTips.left + nWidth;

                TQRLogin.pTipsDlg:MoveEx(rtTips,TRUE);
                ui_utility.setproperty_by_id(TQRLogin.pTipsDlg:GetControlID(), "MoveWith", "dlgLogin");
            end;
            TQRLogin.ShowQRLoadingView();
            TQRLogin.pTipsDlg:Show(TRUE);
        else
            TGPLog.LogE("QRLogin", "Tips dialog is null.");
        end;        
    end;

    HideView = function()
        if nil ~= TQRLogin.pTipsDlg then
            TQRLogin.pTipsDlg:Show(FALSE);
        end;

        if nil ~= TQRLogin.pQRView then
            TQRLogin.pQRView:Show(FALSE);
        end;
    end;

    -- 设置状态
    SetStatus = function(zParam)
    end;

    ChangeImage = function(sImage)
        if nil ~= TQRLogin.pQRImageBack then
            ui_utility.setproperty_by_id(TQRLogin.pQRImageBack:GetControlID(), 
                                         "Background", EQRView.sQRImageBackgroundColor);
        end
        if nil ~= TQRLogin.pQRImage then
            sImage = base_utility.get_work_path() .. "\\tpf_ui\\res\\" .. sImage;

            local     sTemp = "ONEIMAGE:".. sImage ..";Stretch;0;"

            ui_utility.setproperty_by_id(TQRLogin.pQRImage:GetControlID(), 
                                         "Background", sTemp);
        end;

        TQRLogin.ShowQRTimeOutTips(false);

        if nil ~= TQRLogin.pQRMask then
            TQRLogin.pQRMask:Show(FALSE);
        end;
    end;

    -- 显示QR图片
    ShowQRImage = function()
        TGPLog.LogI("QRLogin", "Show QR image.");

        local txt = "";
        local formatted = true;
        local first_line = LoadStr("STR_LOGIN_QRSCAN_TIPS");
        local sec_line = "";
        
        if nil ~= login_qrcode_config.qrcode_config_table["qrcode_tips"] and 
            "" ~= login_qrcode_config.qrcode_config_table["qrcode_tips"] and
            false == base_utility.is_develop_client() then
            first_line = login_qrcode_config.qrcode_config_table["qrcode_tips"];
            formatted = false;
        end

        local device_xone = comm_center.call_service("Svr_LoginMgr_IsDeviceXOne");
        local is_device_xone = false;
        if device_xone ~= nil and device_xone.is_xone ~= nil and device_xone.is_xone~= 0 then
            first_line = LoadStr("STR_LOGIN_QRSCAN_TIPS_IN_XONE");
            formatted = false;
            is_device_xone = true;
        end;

        if nil ~= login_qrcode_config.qrcode_config_table["qrcode_link_tips"] and 
            "" ~= login_qrcode_config.qrcode_config_table["qrcode_link_tips"] and
            false == base_utility.is_develop_client() then
            sec_line = login_qrcode_config.qrcode_config_table["qrcode_link_tips"];
        end

        if is_device_xone then
            sec_line = "";
        end

        txt = first_line;

        if not formatted then
            txt = string.format(EQRView.sQRTipsFormat, first_line, sec_line);
        end

        TQRLogin.ShowQRTips(txt);    -- EQRTipText.sScanTips);
        TQRLogin.ChangeImage("login\\GUARDQRPic.png");
        if nil ~= TQRLogin.nQRIcon and 0 ~= TQRLogin.nQRIcon then
            TQRLogin.SetQRIcon(login_qrcode_config.qrcode_config_table.qrcode_icon);
        end
    end;

    ShowQRConfirm = function()
        TGPLog.LogI("QRLogin", "Show QR confirm.");
        TQRLogin.ShowQRTips(EQRTipText.sWaitingConfirm);
    end;

    ShowQRTimeOut = function()
        TQRLogin.bTimeout = true;

        TGPLog.LogI("QRLogin", "Show QR timeout.");
        TQRLogin.ShowQRTimeOutTips(true);
        if nil ~= TQRLogin.pQRMask then
            TQRLogin.pQRMask:Show(TRUE);
        end;
    end;

    ShowQRFailed = function()
        TQRLogin.bTimeout = true; -- same as timeout
        TGPLog.LogI("QRLogin", "Show QR failed.");
        TQRLogin.ShowQRTimeOutTips(true, true);
        if nil ~= TQRLogin.pQRImage then
            ui_utility.setproperty_by_id(TQRLogin.pQRImage:GetControlID(), 
                                         "Background", EQRView.sQRLoadFailedBackground);
        end
        if nil ~= TQRLogin.pQRMask then
            TQRLogin.pQRMask:Show(TRUE);
        end;
    end;

    ShowRQChecking = function()
        TGPLog.LogI("QRLogin", "Show QR checking.");
        local txt = LoadStr("STR_LOGIN_QRSCAN_TIPS");
        if nil ~= login_qrcode_config.qrcode_config_table["qrcode_tips"] and 
            "" ~= login_qrcode_config.qrcode_config_table["qrcode_tips"] then
            txt = login_qrcode_config.qrcode_config_table["qrcode_tips"];
        end
        TQRLogin.ShowQRTips(txt);    -- EQRTipText.sScanTips);
        TQRLogin.ChangeImage("login\\qrcode_check.png");
    end;

    ShowQRTips = function(sTips)
        if nil ~= TQRLogin.pQRTipsBack then
            TQRLogin.pQRTipsBack:Show(TRUE);
        end

        if nil ~= TQRLogin.pQRTips then
            local sTemp = U8toCUIStr(sTips);
            TQRLogin.pQRTips:Clear();
            TQRLogin.pQRTips:PushBackHtmlString(sTemp);
        end;
    end;

    ShowQRTimeOutTips = function(bShow, bError)
        if nil == TQRLogin.pQRTimeOutTips then
            return
        end

        if bShow == true then 
            local tips = LoadStr("STR_LOGIN_QR_TIMEOUT");
            if true == bError then
                tips = EQRTipText.sGetQRFail;
            end
            local sTemp = U8toCUIStr(tips);
            TQRLogin.pQRTimeOutTips:Clear();
            TQRLogin.pQRTimeOutTips:PushBackHtmlString(sTemp);
            TQRLogin.pQRTimeOutTips:Show(TRUE);
        else
            TQRLogin.pQRTimeOutTips:Show(FALSE);
        end
    end;

    ShowQRBMTips = function(bShow, sTips)
        if TQRLogin.pQRBMTips ~= nil then
            if bShow then
                local sTemp = U8toCUIStr(sTips);
                TQRLogin.pQRBMTips:Show(TRUE);
                TQRLogin.pQRBMTips:SetText(sTemp);
            else
                TQRLogin.pQRBMTips:Show(FALSE);
            end
        end
    end;

    -- 返回
    OnClkBack = function(pUILib, nBtnID)
        comm_center.call_service("Svr_StopQRLogin", {});
        -- comm_center.call_service("Svr_LoginGoBack", {});
    end;

    -- 刷新QRCode
    OnClkQRImage = function(pUILib, nBtnID)
        if TQRLogin.bTimeout then
            TQRLogin.bTimeout = false;
            comm_center.call_service("Svr_RefreshQR", {});
            TQRLogin.ShowQRLoadingView();
        end;
    end;

    -- 点击超时刷新链接
    OnClkTimeOutLink = function(pUILib, nCtrlID, rtObjt, nLink, sText, nX, nY, 
                         cKey, bCtrl, bShift, bAlt, sLink)
        TQRLogin.OnClkQRImage(pUILib, nCtrlID);
    end;

    -- 点推广链接
    OnClkLink = function(pUILib, nCtrlID, rtObjt, nLink, sText, nX, nY, 
                         cKey, bCtrl, bShift, bAlt, sLink)
        local url = login_qrcode_config.qrcode_config_table.qrcode_link or "";
        if "" ~= url then
            web_utility.open_browser(url, nil, true);
        end
    end;

    SetQRIcon = function (url)
        if nil ~= url and "" ~= url then
            url = "ONEIMAGE:client_login\\tlogo.png|"
                .. url .. ";Stretch;0;";            
        else
            url = "ONEIMAGE:client_login\\tlogo.png;Stretch;0;";
        end;
        ui_utility.set_visible(TQRLogin.nQRIcon, true);
        ui_utility.setproperty_by_id(TQRLogin.nQRIcon, "Background", url);
    end;

    ShowQRLoadingView = function()
        -- 隐藏头部提示
        TQRLogin.ShowQRTimeOutTips(false);
        -- 隐藏遮罩
        if nil ~= TQRLogin.pQRMask then
            TQRLogin.pQRMask:Show(FALSE);
        end
        -- 清除背景
        if nil ~= TQRLogin.pQRImageBack then
            ui_utility.setproperty_by_id(TQRLogin.pQRImageBack:GetControlID(), 
                                         "Background", "NONE:");
        end
        -- 隐藏图标
        if nil ~= TQRLogin.nQRIcon and Tenio.INVALID_CONTROLID ~= TQRLogin.nQRIcon then
            ui_utility.set_visible(TQRLogin.nQRIcon, false);
        end
        -- 显示加载动画
        if nil ~= TQRLogin.pQRImage then
            ui_utility.setproperty_by_id(TQRLogin.pQRImage:GetControlID(), 
                                         "Background", EQRView.sQRLoadingBackground);
        end
        -- 隐藏底部文字背景
        if nil ~= TQRLogin.pQRTipsBack then
            TQRLogin.pQRTipsBack:Show(FALSE);
        end
    end;

    nRootID  = 0;

    pQRView  = nil;
    pQRImage = nil;
    pQRMask  = nil;
    pQRTips  = nil;
    pQRTimeOutTips = nil;
    pQRBMTips = nil;

    pTipsDlg = nil;

    bTimeout = false;
    nQRIcon  = 0;

};